package com.android.gufengmall.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.FolkCraftAdapter;
import com.android.gufengmall.bean.FolkCraftBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.FolkCraftBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 民间工艺知识专区
 */
public class FolkCraftFragment extends Fragment implements FolkCraftAdapter.CommentListener {

    private RecyclerView mRvCraft;

    private ArrayList<FolkCraftBean> mFolkCraftBeans;
    private FolkCraftAdapter mFolkCraftAdapter;
    private App mMyApp;
    private DaoSession mDaoSession;
    private FolkCraftBeanDao mFolkCraftBeanDao;

    public FolkCraftFragment() {
    }

    public static FolkCraftFragment newInstance() {
        return new FolkCraftFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_folkcraft, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        mRvCraft = view.findViewById(R.id.rv_craft);
    }

    private void initData() {
        mFolkCraftBeans = new ArrayList<>();

        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mFolkCraftBeanDao = mDaoSession.getFolkCraftBeanDao();

        List<FolkCraftBean> folkCraftBeans = mFolkCraftBeanDao.loadAll();
        mFolkCraftBeans.addAll(folkCraftBeans);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvCraft.setLayoutManager(layoutManager);

        mFolkCraftAdapter = new FolkCraftAdapter(requireContext(), mFolkCraftBeans);
        mFolkCraftAdapter.setOnCommentListener(this);
        mRvCraft.setAdapter(mFolkCraftAdapter);

    }

    @Override
    public void onLikeListener(int position) {
        FolkCraftBean folkCraftBean = mFolkCraftBeanDao.loadByRowId(position);
        boolean isLike = folkCraftBean.getIsLike();
        folkCraftBean.setIsLike(!isLike);
        mFolkCraftBeanDao.update(folkCraftBean);
        if (isLike) {
            Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
        }
        mFolkCraftAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCollectListener(int position) {
        FolkCraftBean folkCraftBean = mFolkCraftBeanDao.loadByRowId(position);
        boolean isCollect = folkCraftBean.getIsCollect();
        folkCraftBean.setIsCollect(!isCollect);
        mFolkCraftBeanDao.update(folkCraftBean);
        if (isCollect) {
            Toast.makeText(requireContext(), "取消收藏", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "收藏成功", Toast.LENGTH_SHORT).show();
        }
        mFolkCraftAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemSelectListener(String position) {
        Intent intent = new Intent(requireContext(), CommentActivity.class);
        intent.putExtra("status", 2);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}